package com.zzzzzz.core.service;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zzzzzz.common.id.UUIDGenerator;
import com.zzzzzz.core.dao.TicketDao;
import com.zzzzzz.core.exception.SystemException;
import com.zzzzzz.core.exception.ZzzzzzException;
import com.zzzzzz.core.model.po.Ticket;

@Service
public class TicketManager {

	@Resource
	private TicketDao ticketDao;

	@Resource
	private ClassNameManager classNameManager;

	public Ticket add(String className, long classPK, String category, Date expirationDate) throws SystemException {
		Date now = new Date();
		long classNameId = classNameManager.getClassNameId(className);
		
		Ticket ticket = new Ticket();
		ticket.setCreateDate(now);
		ticket.setClassPK(classPK);
		ticket.setCategory(category);
		ticket.setClassNameId(classNameId);
		ticket.setKey(UUIDGenerator.get());
		ticket.setExpirationDate(expirationDate);
		
		ticketDao.add(ticket);
		return ticket;
	}
	
	public Ticket findByKey(String key) throws ZzzzzzException, SystemException {
		return ticketDao.findByKey(key);
	}

	public Ticket findByKeyAndCategory(String key, String category) throws ZzzzzzException, SystemException {
		return ticketDao.findByKeyAndCategory(key, category);
	}

	public void delete(String key) throws SystemException {
		ticketDao.delete(key);
	}
	
}
